<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <ul id="tree" class="dtree" data-id="0"></ul>
                    </div>
                </div>


                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="role">
                            <option value="">角色</option>
                            <option value="1">普通用户</option>
                            <option value="2">HR</option>
                            <option value="3">老师</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="key" placeholder="工号|姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchAdd">
        <i class="layui-icon layui-icon-add-1"></i>
        选择
    </button>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common', 'dtree'], function() {
        let table = layui.table;
        let form = layui.form;
        let dtree = layui.dtree;
        let $ = layui.jquery;
        let common = layui.common;
        let class_id = "{$class_id}";

        dtree.render({
            elem: "#tree",
            initLevel: "1",
            method: 'get',
            url: "/Members/getDepartment",
            select: true,
            selectTips: '部门',
        });

        let cols = [
            [{
                type: 'checkbox',
            },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center',
                },
                {
                    title: '工号',
                    field: 'carolid',
                    align: 'center',
                },
                {
                    title: 'Division',
                    field: 'division',
                    align: 'center',
                },
                {
                    title: '部门',
                    field: 'department_name',
                    align: 'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Course/getUserLoad',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 5,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 5,
            })
            return false;
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchAdd') {
                window.batchAdd(obj);
            }
        });


        window.batchAdd = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要添加选中的员工吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Classes/saveUser",
                    data:{id:checkIds, class_id:class_id},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                parent.layer.close(parent.layer.getFrameIndex(window
                                    .name)); //关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
